using System;
using System.Security.Principal;
using SubSonic.SqlGeneration.Schema;

namespace CSI.Directory.Core.Model
{
    [Serializable]
    public class User : Base, IPrincipal
    {
        public User(IIdentity identity, string[] roles)
        {
            Identity = identity;
            
        }
        public User(){}

        public string FirstName	 { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int Option { get; set; }
        public int Package { get; set; }
        public string Password { get; set; }
        public string Salt { get; set; }
        public IIdentity Identity { get; private set; }
        public string Roles { get; set; }
        public bool KeepLoggedIn { get; set; }
        public bool Featured { get; set; }
        public bool SetUpComplete { get; set; }
        [SubSonicIgnore]
        public bool IsListed { get; set; }

        public bool IsInRole(string role)
        {
            string[] rolesArray = Roles.Split(';');
            Array.Sort(rolesArray);
            return Array.BinarySearch(rolesArray, role) >= 0 ? true : false;

        }
        public bool IsInRole(string[] roles)
        {
            string[] rolesArray = Roles.Split(';');
            Array.Sort(rolesArray);
            var result = false;
            foreach (var role in roles)
            {
                if (Array.BinarySearch(rolesArray, role) < 0) continue;
                result = true;
                break;
            }
            return  result;

        }
        
    }
}